package entity

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"github.com/jinzhu/gorm"
)

type GroupIds []uint

func (gids GroupIds) Value() (driver.Value, error) {
	return json.Marshal(gids)
}

func (gids *GroupIds) Scan(v interface{}) error {
	return json.Unmarshal(v.([]byte), gids)
}

type User struct {
	gorm.Model
	Account    string `json:"account"`
	Password   string `json:"password"`
	Salt       string `json:"salt"`
	Nickname   string `json:"nickname"`
	Sex        uint8  `json:"sex"`
	Phone      string `json:"phone" valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email      string `json:"email" valid:"email"`
	LoginTime  uint64 `json:"login_time"`
	LogoutTime uint64 `json:"logout_time"`
	ClientIp   string `json:"client_ip"`
	ClientPort string `json:"client_port"`
	UserStatus uint8  `json:"user_status"`
	// 加入的群的id
	Group_id GroupIds `json:"group_id" gorm:"type:json"`
}

func (table *User) TableName() string {
	return "user"
}

func (u User) String() string {
	return fmt.Sprintf("User{ID: %d, Account: %s, Nickname: %s, Phone: %s, Email: %s, Status: %d}",
		u.ID, u.Account, u.Nickname, u.Phone, u.Email, u.UserStatus)
}
